const service = require('../groupBuyService.js');
const utilService = require('../../../../utils/utilService.js');

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //轮播图参数
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    shareMsg:"",
    groupBuyId:"",
    hasNextPage: true, //更多是否有下一页
    swiperCurrent: 1,          // 轮播图当前的数量
    // 接口数据
    groupBuyGoodsVo: {},//商品详情
    /**商品详情参数
     * 
     * goodsUrl         string商品图片
       goodsName        string商品名称
       saledCount       integer已送件数
       save             integer节省
       integral         integer花费积分
       groupPeopleNuber integer开团人数
       goodsId          string商品id
       count            string共多少件
       total            string已拼成团数
       description      string商品描述
       postage          number邮费
     */
    playMethod: "",
    rule: '1.提前挑选手机单离开 发的房间号积分兑换  东方红 电话费很多事了收到  返回发沙发发 \n 2.商品 \n 3.每天',
    logo: "http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg",//商家LOGO
    logoName: "",//商家名称
    logoRegion: "",//商家区域
    attention: 0,//是否关注(0：未关注 1：已关注)
    groupBuyUserVos: [],//领取成功用户
    /**领取成功用户信息
     * userId       string用户id
     * userUrl      string用户头像
     * userName     string用户名称
     * userRegion   string用户区域
     * time         integer领取时间
     * count        integer个数
     */
    productUrls:[],//轮播图数组
    videoUrl: '', //播放视频地址
    starPage: 0,
    bottmBtnTitle:"",
    playMethods: [
      {
        url: '../../../images/shangou/1-.png',
        isExpire: true
      },
      {
        url: '../../../images/shangou/2-.png',
        isExpire: true
      },
      {
        url: '../../../images/shangou/3-.png',
        isExpire: true
      }
    ],
    firstStep:"",
    secStep: "",
    thirdStep: "",
    myOptions: null,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      myOptions: options
    })
    var self = this;
    //授权
    self.authAlert = self.selectComponent("#authAlert");
    self.authAlert.bindCatch()
    app.loginPromise.then(() => {
      self.moreList = this.selectComponent("#more-list");
      self.loadData(options)
    });

  },
  authSuccess() {
    this.moreList = this.selectComponent("#more-list");
    var options = this.data.myOptions
    this.loadData(options)
  },
  loadData: function (options){
    var self = this;
    service.groupbuyGoodsDetail(options["gBId"], function (data) {
      if (data.groupBuyGoodsVo) {
        self.setData({
          productUrls: data.groupBuyGoodsVo.goodsUrlList,
          groupBuyGoodsVo: data.groupBuyGoodsVo,
          groupBuyUserVos: data.groupBuyUserVos,
          logo: data.logo,
          logoName: data.logoName,
          logoRegion: data.logoRegion,
          playMethod: data.playMethod,
          status: data.groupBuyGoodsVo.status,
          bottmBtnTitle: data.groupBuyGoodsVo.status == -1 ? "已下架" : data.groupBuyGoodsVo.integral + "果冻开团",
        })
      }
    });
    self.getShareMessage(4);
    self.setData({
      windowHeight: wx.getStorageSync('windowHeight')
    });
    self.comment = self.selectComponent("#comment");
    self.comment.requestComment(options["goodsId"]);
    // 获取video实例
    self.showVideo = self.selectComponent("#showVideo");
    self.setData({
      groupBuyId: options["gBId"],
    })
  

    service.getRule(4, function (res) {
      self.setData({
        firstStep: res.value,
      });
    });
    service.getRule(11, function (res) {
      self.setData({
        secStep: res.value,
      })
    });
    service.getRule(12, function (res) {
      self.setData({
        thirdStep: res.value,
      })
    });
    service.getRule(5, function (res) {
      self.setData({
        rule: res.value,
      })
    });
  },

  onShow: function (options){

  },

  playVideo:function(e){
    let videoUrl = this.data.productUrls[e.currentTarget.dataset.index].video;
    this.setData({
      videoUrl: videoUrl
    });
    this.showVideo.show()
  },
  /**
   * 视频取消回调
   */
  videoViewDimiss: function () {
    this.setData({
      isStartVisible: false,
    })
  },
  /**
   * view触底监听
   */
  onReachBottom: function () {
    this.moreList.reachBottom()
  },
  submitForm:function(e){
    // 获取表单id
    let formId = e.detail.formId;
    getApp().saveUserFormId(formId, 9);
    console.log("formId-9", formId)
  },

  openGroupBuy: function(e){
    wx.navigateTo({
      url: '../groupBuyOrder/groupBuyOrder?groupBuyId=' + this.data.groupBuyId,
    })
  },

  //获取分享信息
  getShareMessage: function (type) {
    var self = this;
    utilService.shareSettings(type,
      function (data) {
        console.log("分享数据", data)
        self.setData({
          shareMsg: data.title
        })
      },
      function (error) {

      }
    );
  },
  /**
   * 用户点击分享
   */
  onShareAppMessage: function () {
    var title = this.data.shareMsg;
    title = title.replace(/#/, app.globalData.userInfo.nickName)
    title = title.replace(/{}/, this.data.groupBuyGoodsVo.goodsName)
    var goodsUrl = this.data.groupBuyGoodsVo.goodsUrl;
    var param = '?uId=' + getApp().globalData.userId + '&gBId=' + this.data.groupBuyId + '&goods_id=' + this.data.groupBuyGoodsVo.goodsId + "&gBUId=" + this.data.groupBuyUserId
    var path = 'redPacket/pages/groupBuy/groupBuyGoodsDetail/groupBuyGoodsDetail' + param
    return {
      title: title,
      path: path,
      imageUrl: goodsUrl,
      success: function (res) {
        // 需要在页面onLoad()事件中实现接口
        wx.showShareMenu({
          // 要求小程序返回分享目标信息
          withShareTicket: true
        });
      },
      fail: function (res) {
      }
    }
  },
  //点击赚花
  zhuanhuaClick: function (e) {
    wx.navigateTo({
      url: '/mineSubs/pages/getFlowers/getFlowers',
    })
  },
  //返回首页
  backHome: function (e) {
    wx.reLaunch({
      url: '../../../../pages/home/home',
    })
  },
  imageClick:function(e){
    let current = this.data.swiperCurrent - 1;
    let item = this.data.productUrls[current]
    // 视频播放
    if (item.video) {
      this.setData({
        autoplay: false
      })
      this.showVideo.show()
    } else {
    let urls = []
    for (let i = 0; i < this.data.productUrls.length; i++) {
      let img = this.data.productUrls[i].img
      if (this.data.productUrls[i].video == "") {
        let str = img.replace("zoom/", "")
        urls.push(str)
      } else {
        urls.push(img)
      }
    }
    // 图片预览
    wx.previewImage({
      current: item.img.replace("zoom/", ""),
      urls: urls
    })
    }
  },
  swiperEvent: function (e) {
    let swiperCurrent = e.detail.current + 1
    // 设置播放按钮是否显示/ 当前播放的坐标
    this.setData({
      swiperCurrent: swiperCurrent,
    })
  },
})